package com.wanpinghui.supplier.widgets;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.Layout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wanpinghui.supplier.R;

import org.xutils.common.util.DensityUtil;


/***
 * 功能描述: 标题
 * 作者:huangyongcan
 * 时间:2016/8/10
 * 版本:
 ***/
public class TitlePanelLayoutUser extends LinearLayout {
    private Context mContext;
    private View mView;
    private FrameLayout fl_title_area;


    private ImageView mViewBacktrack;
    private FrameLayout fl_right_area;
    private TextView mTextViewRight;
    private int rightIcon;

    private boolean isbacktrack;
    private String title;//标题(文字)
    private TextView mTextViewTitle;
    private ImageView mImageViewAdd;

    private int statubg;
    private int titlecolor;

    private SpinView sv;

    private RelativeLayout rl_title_area;


    public TitlePanelLayoutUser(Context context) {
        super(context);
    }

    public TitlePanelLayoutUser(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        TypedArray mTypedArray = getResources().obtainAttributes(attrs,
                R.styleable.TitPanelLayout);
        title = mTypedArray.getString(R.styleable.TitPanelLayout_titles);
        isbacktrack = mTypedArray.getBoolean(R.styleable.TitPanelLayout_back, true);
        rightIcon = mTypedArray.getResourceId(R.styleable.TitPanelLayout_righticon, 0);
        statubg = mTypedArray.getColor(R.styleable.TitPanelLayout_statubg, getResources().getColor(R.color.colorBgStatusBar));
        titlecolor = mTypedArray.getColor(R.styleable.TitPanelLayout_titlecolor, getResources().getColor(R.color.colorCommonTitle));
        init();
        mTypedArray.recycle();
    }

    private void init() {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        mView = inflater.inflate(R.layout.view_title_panel_user, this);
        fl_title_area = (FrameLayout) mView.findViewById(R.id.fl_title_area);
        rl_title_area = (RelativeLayout) mView.findViewById(R.id.rl_title_area);
        mTextViewTitle = (TextView) mView.findViewById(R.id.tv_title_panel);
        mViewBacktrack = (ImageView) mView.findViewById(R.id.mViewBacktrack);
        fl_right_area=(FrameLayout)mView.findViewById(R.id.fl_right_area);
        sv = (SpinView) mView.findViewById(R.id.sv);

        mTextViewRight = (TextView) mView.findViewById(R.id.tv_toolbar_right);
        mImageViewAdd = (ImageView) mView.findViewById(R.id.iv_addition);
        setStatuBg(statubg);
        setCenterTitleTextColor(titlecolor);
        mTextViewTitle.setText(title);
        if (isbacktrack) {
            mViewBacktrack.setVisibility(View.VISIBLE);
        } else {
            mViewBacktrack.setVisibility(View.GONE);
        }

        if (rightIcon != 0) {
            mImageViewAdd.setVisibility(VISIBLE);
            mImageViewAdd.setImageResource(rightIcon);
        } else {
            mImageViewAdd.setVisibility(GONE);
        }

//        if (isaddition) {
//            mViewAboAdd.setVisibility(View.VISIBLE);
//        } else {
//            mViewAboAdd.setVisibility(View.GONE);
//        }
//        if (TextUtil.isNotEmpty(str_right)) {
//            mTextViewRight.setText(str_right);
//            mTextViewRight.setTextColor(righttxtcolor);
//            mTextViewRight.setVisibility(View.VISIBLE);
//        } else {
//            mTextViewRight.setVisibility(View.GONE);
//        }
//        if (issearch) {
//            mLineaLayoutSearch.setVisibility(View.VISIBLE);
//        } else {
//            mLineaLayoutSearch.setVisibility(View.GONE);
//        }
    }

    public void setOnClickSearch(OnClickListener ClickListener) {
        if (ClickListener != null) {
            //  mLineaLayoutSearch.setOnClickListener(ClickListener);
        }
    }

    public void setRightText(String text) {
        mTextViewRight.setText(text);
    }

    public void setRightTextColor(int color) {
        mTextViewRight.setTextColor(color);
    }

    public void setOnClickAddition(OnClickListener addClickListener) {
        if (addClickListener != null) {
            mImageViewAdd.setOnClickListener(addClickListener);
        }
    }


    public void hideRightText() {
        mTextViewRight.setVisibility(GONE);
    }

    public void showRightText() {
        fl_right_area.setVisibility(View.VISIBLE);
        mTextViewRight.setVisibility(VISIBLE);
    }

    public void hideBack() {
        mViewBacktrack.setVisibility(INVISIBLE);
    }

    public void showBack() {
        mViewBacktrack.setVisibility(VISIBLE);
    }

    public void setBackRes(Drawable drawable) {
        mViewBacktrack.setImageDrawable(drawable);
    }

    public void showSpinView() {
        sv.setVisibility(VISIBLE);
    }

    public void hideSpinView() {
        sv.setVisibility(INVISIBLE);
    }


    public void setOnClickBacktrack(final OnClickListener clickListener) {
        if (clickListener != null) {
            mViewBacktrack.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    hideSofeInput(v);
                    clickListener.onClick(v);
                }
            });
        }
    }

    public void setAddViewRes(Drawable drawable) {
        if (drawable != null) {
            mImageViewAdd.setVisibility(VISIBLE);
            mImageViewAdd.setImageDrawable(drawable);
        } else {
            mImageViewAdd.setVisibility(GONE);
        }
    }

    public void setOnClickRightTxt(final OnClickListener clickListener) {
        if (clickListener != null) {
            mTextViewRight.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    hideSofeInput(v);
                    clickListener.onClick(v);
                }
            });
        }
    }

    private void resetTitleTextShow(String title) {
        TextPaint paint = mTextViewTitle.getPaint();
        float size = Layout.getDesiredWidth(title, 0, title.length(), paint);
        float width = DensityUtil.getScreenWidth() - DensityUtil.dip2px(110);
        int left = 0;
        float l1 = (float) (0.8 * width);
        float l2 = (float) (0.4 * width);
        float l3 = (float) (0.3 * width);
        if (size > l1) left = 110;
        else if (size > l2) left = 60;
        else if (size > l3) left = 30;
        rl_title_area.setPadding(DensityUtil.dip2px(left), 0, 0, 0);
    }

    public void setBackGround(int id) {
        fl_title_area.setBackgroundResource(id);
    }

    private void hideSofeInput(View view) {
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }


    public void setTitle(String s) {
        this.title = s;
        mTextViewTitle.setText(this.title);
    }

    public void setStatusVisible(boolean isVisible) {
        if (isVisible) {
            mView.setVisibility(View.VISIBLE);
        } else {
            mView.setVisibility(View.GONE);
        }

    }

    public void setStatuBg(int color) {
        mView.setBackgroundColor(color);
    }

    public void setTitleBg(int color) {
        fl_title_area.setBackgroundColor(color);
    }

    public void setCenterTitleTextColor(int color) {
        mTextViewTitle.setTextColor(color);
        //   mTextViewRight.setTextColor(color);
    }

    public void setSaveText(CharSequence text) {
        mTextViewRight.setText(text);
    }

    public void setOnClickSave(OnClickListener ClickListener) {
        if (ClickListener != null) {
            mTextViewRight.setOnClickListener(ClickListener);
        }

    }

    public void setSaveEnabled(final boolean enabled) {
        ((Activity) mContext).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mTextViewRight.setEnabled(enabled);
            }
        });

    }
}
